package d.max.rwp.ui;

import android.view.View;
import android.view.View.OnClickListener;
import d.max.rwp.action.Action;
import d.max.rwp.ui.component.ImageViewCheckable;
import java.util.HashMap;
import java.util.Map;

/**
 * author: Maxim Dybarskiy
 * date:   Aug 28, 2011
 * time:   3:15:12 PM
 */
public class ViewSwitcher {
    
    private Map<ImageViewCheckable, Action> viewes = new HashMap<ImageViewCheckable, Action>();
    private OnClickListener listener  = new OnClickListener() {
        @Override
        public void onClick(View view) {
            //            ImageViewCheckable ivc = (ImageViewCheckable) view;
            //            // switch off other views if checked
            //            for (ImageViewCheckable v : viewes.keySet()) {
            //                if (!v.equals(ivc) && v.isChecked()) {
            //                    v.setChecked(false);
            //                }
            //            }
            //            // switch on and perform action if not checked
            //            if (!ivc.isChecked()) {
            //                ivc.setChecked(true);
            //                viewes.get(view).perform();
            //            }
            switchOn(view);
        }
    };
    
    // temp
    public void switchOn(View view) {
        ImageViewCheckable ivc = (ImageViewCheckable) view;
        // switch off other views if checked
        for (ImageViewCheckable v : viewes.keySet()) {
            if (!v.equals(ivc) && v.isChecked()) {
                v.setChecked(false);
            }
        }
        // switch on and perform action if not checked
        if (!ivc.isChecked()) {
            ivc.setChecked(true);
            viewes.get(view).perform();
        }
    }
    
    public void add(ImageViewCheckable view, Action action) {
        viewes.put(view, action);
        view.setOnClickListener(listener);
    }
    
    public void remove(ImageViewCheckable view) {
        view.setOnClickListener(null);
        viewes.remove(view);
    }
    
    public boolean contains(ImageViewCheckable view) {
        return viewes.keySet().contains(view);
    }
}
